package com.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.model.Ncp;
import com.base.service.INcpService;
import com.base.util.FileDownloadUtil;
import com.base.util.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 
 * 请在这里补充你的控制器代码。
 *
 */
@Controller
public class MyController {
	@Autowired
	INcpService iNcpService;
	Log log = LogFactory.getLog(getClass());
	
	@RequestMapping("/myRequestHandle")
	public String myRequestHandle(String arg) {
		log.info("请在这里补充你的控制器代码...");
		return "/myView.jsp";
	}

	@CrossOrigin
	@RequestMapping("/download")
	public void download(HttpServletResponse response, @RequestParam("ncpid") String ncpid, HttpSession session){
		Ncp ncpid1 = iNcpService.getOne(new QueryWrapper<Ncp>().eq("ncpid", ncpid));
		String imgUrl = ncpid1.getImgUrl();
		// 获取模板存储路径
		String path = imgUrl;
		int i = path.lastIndexOf("base")+11;
		path=path.substring(i,path.length());
		try {
			FileDownloadUtil.fileDownload(path, response,session);
		} catch (IOException e) {
			System.err.println("模板下载失败!");
		}
	}


}
